/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.StorehouseBean;
import com.minierp.dao.StockDao;
import com.minierp.dao.StorehouseDao;
import com.minierp.dao.TallySetDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Storehouse;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class StorehouseMACtr extends MultiActionController {
	private StorehouseDao storehouseDao;
	private TallySetDao tallySetDao;
	private StockDao stockDao;
	private String listView;
	private String listForSelectView;
	private String operateSuccessView;

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "BASE_STOR")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listView);
			List list = this.storehouseDao.getList(true);
			List tsList = this.tallySetDao.getList(false);
			ArrayList arr = new ArrayList();

			for (int i = 0; i < list.size(); ++i) {
				Storehouse s = (Storehouse) list.get(i);
				StorehouseBean bean = new StorehouseBean();
				bean.setStorehouse(s);
				bean.setTallySetName(EncCommonHelper.getSimpleObjectName(
						tsList, s.getTallySetId()));
				arr.add(bean);
			}

			mv.addObject("list", arr);
			return mv;
		}
	}

	public ModelAndView handleListForSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForSelectView);
		List list = this.storehouseDao.getList(true);
		mv.addObject("list", list);
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.operateSuccessView);
		String id = request.getParameter("id");
		if (this.stockDao.isStorehouseHasStock(Long.parseLong(id))) {
			EncCommonHelper.setAlertMessage(request, "有库存存在，不能删除！");
		} else {
			Storehouse s = (Storehouse) this.storehouseDao.get(
					Storehouse.class, Long.parseLong(id));
			if (s != null) {
				s.setEnabled("N");
				this.storehouseDao.update(s);
			}

			EncCommonHelper.setAlertMessage(request, "删除成功！");
		}

		return mv;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}

	public String getOperateSuccessView() {
		return this.operateSuccessView;
	}

	public void setOperateSuccessView(String operateSuccessView) {
		this.operateSuccessView = operateSuccessView;
	}

	public String getListForSelectView() {
		return this.listForSelectView;
	}

	public void setListForSelectView(String listForSelectView) {
		this.listForSelectView = listForSelectView;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}
}